package com.pengli.mytools.ui.mainPage.home;

import android.content.Context;
import android.content.Intent;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.pengli.mytools.MyApplication;
import com.pengli.mytools.R;
import com.pengli.mytools.adpter.HomeGridAdapter;
import com.pengli.mytools.beans.po.URLData;
import com.pengli.mytools.greendao.URLDataDao;
import com.pengli.mytools.ui.base.BaseActivity;
import com.pengli.mytools.ui.webView.MyWebViewActivity;
import com.pengli.mytools.views.dialog.MyPopDialog;

import org.greenrobot.greendao.query.Query;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HomeDetailActivity extends BaseActivity {

    @BindView(R.id.ac_home_gridview)
    GridView gridView;

    @BindView(R.id.image_back)
    ImageView back;

    @BindView(R.id.title)
    TextView title;

//    @BindView(R.id.btn_confirm)
//    Button confirm;


    private List<URLData> list = new ArrayList<>();
    private HomeGridAdapter adapter;
    private String type;
    private URLDataDao urlDataDao;
    private Context context;
    private MyPopDialog dialog;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_home_detail);
        ButterKnife.bind(this);

        context=getApplicationContext();
        Intent intent = getIntent();
        type = intent.getStringExtra("type");
        title.setText(type);

        dialog=new MyPopDialog(context,R.layout.dialog_confirm);

    }

    @Override
    protected void initData() {

        urlDataDao = MyApplication.getInstance().getDaoSession().getURLDataDao();

        Query<URLData> query = urlDataDao.queryBuilder()
                .where(URLDataDao.Properties.UrlType.eq(type))
                .build();
        list = query.list();

        adapter = new HomeGridAdapter(HomeDetailActivity.this, this.list);
        gridView.setAdapter(adapter);
    }

    @Override
    protected void setListener() {
        gridView.setOnItemClickListener(
                (parent, view, position, id) -> {
                    Intent intent = new Intent(HomeDetailActivity.this, MyWebViewActivity.class);
                    intent.putExtra("urldata", list.get(position));
                    startActivity(intent);
                });

        gridView.setOnItemLongClickListener((parent, view, position, id) -> {

            dialog.show();

            return true;
        });

//        confirm.setOnClickListener(v -> {
//            urlDataDao.delete();
//        });

        back.setOnClickListener(
                v -> {
                    HomeDetailActivity.this.finish();
                });
    }
}
